<%-- 
    공통코드 처리
--%><%@ page contentType="text/html; charset=utf-8" 
%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" 
%><%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" 
%><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" 
%><%@ page import="com.helloworld.common.service.CommonCodeService"
%><%@ page import="java.util.List"
%><%@ page import="java.util.Map"
%><%@ page import="com.cfw.util.SpringUtil"
%><%
    String gp = request.getParameter("gp");
    CommonCodeService service = (CommonCodeService)SpringUtil.getBean("commonCodeService");
    Map<String, List<Map<String, String>>> codes = service.getCodeList(gp);
%><script type="text/javascript">
var commonCode = [
<%
    StringBuilder builder = new StringBuilder();
    if (codes != null) {
    	for (String key : codes.keySet()) {
    		List<Map<String, String>> group = codes.get(key);
    	    
    		if (!"".equals(builder.toString())) { builder.append(","); }
    		builder.append("{group:\"").append(key).append("\",data:[");
            for (int i = 0; i < group.size(); i++ ) {
                Map<String, String> map = group.get(i);
                builder.append("{key:\"").append(map.get("key")).append("\",value:\"").append(map.get("value")).append("\"}");
                if (i < group.size() - 1) { builder.append(","); }
            }
            builder.append("]}");
    	}
    }
%><%= builder.toString() %>
];

//코드구분자에 포함되는 코드정보의 [{key:?, value:?}...] 형식 배열 반환 
function getCommonCodeArr(gb) {
    for (var i = 0; i < commonCode.length; i++) {
        if (commonCode[i].group == gb) {
            return commonCode[i].data;
        }
    }
    return;
}

// 코드구분자 및 코드에 해당하는 표시값 반환
function getCommonCodeValue(gb, key) {
    var data = getCommonCodeArr(gb);
    
    for (var i = 0; data && i < data.length; i++) {
        if (data[i].key == key) { return data[i].value; }
    }
    
    return;
}

// 코드구분자에 해당하는 코드, 값으로 셀렉트 박스 html문자열 반환
function getCodeSelectHtml(gb, current) {
    var data = getCommonCodeArr(gb);
    var html = "";
    for (var i = 0; data && i < data.length; i++) {
        if (i == 0) { html += "<select id='code-" + gb + "' name='code-" + gb + "'>"; }
        
        var key = data[i].key;
        var value = data[i].value;        
        
        html += "<option value='" + key + "'" + (current && current == key ? " selected " : "") + ">" + value + "</option>";
    }
    html += "</select>";
    return html;
}
</script>